package orgs.example.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;
import orgs.example.mapper.AdminMapper;
import orgs.example.mapper.CustomerMapper;
import orgs.example.mapper.StaffMapper;
import orgs.example.mapper.UserMapper;
import orgs.example.pojo.Cancellation;
import orgs.example.pojo.MeetingRoom;
import orgs.example.pojo.Reservation;
import orgs.example.pojo.User;
import orgs.example.service.CustomerService;
import orgs.example.utils.EntityToMap;
import orgs.example.utils.ThreadLocalUtil;

import javax.xml.stream.events.EntityDeclaration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ObjectMapper jacksonObjectMapper;


    @Override
    public List<MeetingRoom> searchRoom(MeetingRoom meetingRoom) {
        return adminMapper.searchMeetingRoom(meetingRoom);
    }

    @Override
    public List<Reservation> searchReservationByUserId() {

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("userId");
        List<Reservation> reservations = customerMapper.searchReservationByUserId(id);

        return reservations;
    }

    @Override
    public void changeReservationStatus(Integer reserveId, String status) {
        customerMapper.changeReservationStatus(reserveId,status);
    }

    @Override
    public void changeRoomStatus(Integer roomId, String status) {
        customerMapper.changeRoomStatus(roomId,status);
    }

    @Override
    public Reservation searchReservationByReserveId(Integer reserveId) {
        return staffMapper.searchReserveByreserveId(reserveId);
    }

    @Override
    public MeetingRoom searchRoomByRoomId(Integer roomId) {
        return customerMapper.searchRoomByRoomId(roomId);
    }

    @Override
    public User searchUserByUserId(Integer userId) {
        return userMapper.findByUserId(userId);
    }

    @Override
    public void addCancellation(Cancellation cancellation) {
        customerMapper.addCancellation(cancellation);
    }

    @Override
    public List<Map<String,Object>> searchCancellationByUserId() {

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("userId");

        User user = userMapper.findByUserId(id);

        List<Reservation> reservations = searchReservationByUserId();
        List<Map<String,Object>> cancellations = new ArrayList<>();
        for(Reservation r:reservations) {
            List<Cancellation> cancellations1 = customerMapper.searchCancellationByReserveId(r.getReserveId());

            Integer roomId = r.getRoomId();
            String roomName = customerMapper.searchRoomByRoomId(roomId).getRoomName();
            for(Cancellation c:cancellations1){
                try {

                    Map<String, Object> objectMap = EntityToMap.convertIgnoreNull(c);
                    objectMap.put("userId",id);
                    objectMap.put("userAccount",user.getUserAccount());

                    objectMap.put("roomId",roomId);
                    objectMap.put("roomName",roomName);

                    cancellations.add(objectMap);

                }catch (Exception e){

                }
            }

        }
        return cancellations;
    }

    @Override
    public List<Reservation> searchAllReservation() {
        return customerMapper.searchAllReservation();
    }

    @Override
    public List<Cancellation> searchCancellationByRserveId(Integer reserveId) {
        return customerMapper.searchCancellationByReserveId(reserveId);
    }

    @Override
    public void addReservation(Reservation reservation) {
        customerMapper.addReservation(reservation);
    }

    @Override
    public List<Reservation> searchAllReservationByRoomId(Integer roomId) {
        return customerMapper.searchAllReservationByRoomId(roomId);
    }
}
